package edu.cebanc.spring.biblioteca.controllers;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.cebanc.spring.biblioteca.bean.AuthorBean;
import edu.cebanc.spring.biblioteca.bean.GenreBean;
import edu.cebanc.spring.biblioteca.bean.LanguageBean;
import edu.cebanc.spring.biblioteca.bean.PublisherBean;
import edu.cebanc.spring.biblioteca.services.GeneralDataService;

@Controller
@RequestMapping(value="/data")
public class GeneralDataController {

	@Autowired
	GeneralDataService genDataService;

	@RequestMapping(method=RequestMethod.GET)
	public String getMainView(Model model) {
		return "dataMain";
	}
	
	@RequestMapping(value="/authors", method=RequestMethod.GET)
	public String getAuthorsList(Model model) {
		model.addAttribute("author", genDataService.listAuthors());
		
		return "authorsList";
	}
	@RequestMapping(value="/authorsForm", method=RequestMethod.GET)
	public String getAuthorForm(Model model) {
		
		AuthorBean ab = new AuthorBean();
		model.addAttribute("author", ab);
		
		return "authorsForm";
	}
	@RequestMapping(value="/editAuthor", method=RequestMethod.POST)
	public String getAuthorsData(@Valid AuthorBean ab, BindingResult result) {
		if(result.hasErrors()) {
			return "redirect:/data/authorsForm";
		}
		genDataService.saveAuthor(ab);
		
		return "redirect:/data/authors";
	}
	@RequestMapping(value="/authorInfo/{id}", method=RequestMethod.GET)
	public String getAuthorInfoView(@PathVariable String id, Model model) {
		model.addAttribute("author", genDataService.loadAuthor(id));

		return "authorInfo";
	}
	@RequestMapping(value="/editAuthor/{id}", method=RequestMethod.GET)
	public String editAuthor(@PathVariable String id, Model model) {
		model.addAttribute("author", genDataService.loadAuthor(id));
		
		return "authorsForm";
	}
	@RequestMapping(value="/deleteAuthor/{id}", method=RequestMethod.GET)
	public String deleteAuthor(@PathVariable String id) {
		genDataService.deleteAuthor(id);
		
		return "redirect:/data/authors";
	}

	@RequestMapping(value="/languages", method=RequestMethod.GET)
	public String getLanguagesList(Model model) {
		model.addAttribute("lang", genDataService.listLanguages());
		
		return "languagesList";
	}
	@RequestMapping(value="/languagesForm", method=RequestMethod.GET)
	public String getLanguageForm(Model model) {
		
		LanguageBean lb = new LanguageBean();
		model.addAttribute("lang", lb);
		
		return "languagesForm";
	}
	@RequestMapping(value="/editLanguage", method=RequestMethod.POST)
	public String getLanguagesData(@Valid LanguageBean lb, BindingResult result) {
		if(result.hasErrors()) {
			return "redirect:/data/languagesForm";
		}
		genDataService.saveLanguage(lb);
		
		return "redirect:/data/languages";
	}
	@RequestMapping(value="/languageInfo/{id}", method=RequestMethod.GET)
	public String getLanguageInfoView(@PathVariable String id, Model model) {
		model.addAttribute("lang", genDataService.loadLanguage(id));

		return "languageInfo";
	}
	@RequestMapping(value="/editLanguage/{id}", method=RequestMethod.GET)
	public String editLanguage(@PathVariable String id, Model model) {
		model.addAttribute("lang", genDataService.loadLanguage(id));
		
		return "languagesForm";
	}
	@RequestMapping(value="/deleteLanguage/{id}", method=RequestMethod.GET)
	public String deleteLanguage(@PathVariable String id) {
		genDataService.deleteLanguage(id);
		return "redirect:/data/languages";
	}

	@RequestMapping(value="/genre", method=RequestMethod.GET)
	public String getGenreList(Model model) {
		model.addAttribute("genre", genDataService.listGenres());
		
		return "genreList";
	}
	@RequestMapping(value="/genreForm", method=RequestMethod.GET)
	public String getGenreForm(Model model) {
		GenreBean gb = new GenreBean();
		model.addAttribute("genre", gb);
		
		return "genreForm";
	}
	@RequestMapping(value="/editGenre", method=RequestMethod.POST)
	public String getGenreData(@Valid GenreBean gb, BindingResult result) {
		if(result.hasErrors()) {
			return "redirect:/data/genreForm";
		}
		genDataService.saveGenre(gb);
		
		return "redirect:/data/genre";
	}
	@RequestMapping(value="/genreInfo/{id}", method=RequestMethod.GET)
	public String getGenreInfoView(@PathVariable String id, Model model) {
		model.addAttribute("genre", genDataService.loadGenre(id));

		return "genreInfo";
	}
	@RequestMapping(value="/editGenre/{id}", method=RequestMethod.GET)
	public String editGenre(@PathVariable String id, Model model) {
		model.addAttribute("genre", genDataService.loadGenre(id));
		
		return "genreForm";
	}
	@RequestMapping(value="/deleteGenre/{id}", method=RequestMethod.GET)
	public String deleteGenre(@PathVariable String id) {
		genDataService.deleteGenre(id);
		return "redirect:/data/genre";
	}

	@RequestMapping(value="/publishers", method=RequestMethod.GET)
	public String getPublishersList(Model model) {
		model.addAttribute("publisher", genDataService.listPublishers());
		
		return "publishersList";
	}
	@RequestMapping(value="/publishersForm", method=RequestMethod.GET)
	public String getPublisherForm(Model model) {
		PublisherBean pb = new PublisherBean();
		
		model.addAttribute("publisher", pb);
		
		return "publishersForm";
	}
	@RequestMapping(value="/editPublisher", method=RequestMethod.POST)
	public String getPublishersData(@Valid PublisherBean pb, BindingResult result) {
		if(result.hasErrors()) {
			return "redirect:/data/publishersForm";
		}
		genDataService.savePublisher(pb);
		
		return "redirect:/data/publishers";
	}
	@RequestMapping(value="/publisherInfo/{id}", method=RequestMethod.GET)
	public String getPublisherInfoView(@PathVariable String id, Model model) {
		model.addAttribute("publisher", genDataService.loadPublisher(id));
		//System.out.println(genDataService.loadUser(id).toString());
		return "publisherInfo";
	}
	@RequestMapping(value="/editPublisher/{id}", method=RequestMethod.GET)
	public String editPublisher(@PathVariable String id, Model model) {
		model.addAttribute("publisher", genDataService.loadPublisher(id));
		
		return "publishersForm";
	}
	@RequestMapping(value="/deletePublisher/{id}", method=RequestMethod.GET)
	public String deletePublisher(@PathVariable String id) {
		genDataService.deletePublisher(id);
		return "redirect:/data/publishers";
	}
	
}
